<?php
class TixianAction extends HomeAction {

    public function index(){ 
    	$obj = M('Tixian');
        $map['userid'] = $this->userID;
        $count = $obj->where($map)->count();
        import("@.ORG.WapPage");
        $page = new Page($count, 10);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();       
    }

    public function submit(){
        if(isset($_POST['dosubmit'])) {
            //提交申请
            $obj = D('Tixian');
            if ($data = $obj->create()) {
                $map['userid']=$this->userID;
                $oldMoney = M('Member')->where($map)->getField('money');

                if ($data['money'] > $oldMoney) {
                    $this->error("您可提现佣金不足".$data['money'].'元');
                }

                if ($data['money'] < C('tx_min')) {
                    $this->error("提现金额不能小于".C('tx_min').'元');
                }

                $newMoney = $oldMoney - $data['money'];

                //用户账号减去佣金
                M('Member')->where($map)->setDec('money',$data['money']); 

                $data['oldMoney'] = $oldMoney;
                $data['newMoney'] = $newMoney;

                if ($obj->add($data)) {
                    $this->success("操作成功",U('Tixian/index'));
                }else{
                    $this->error("操作失败");
                }
            }else{
                $this->error($obj->getError());
            } 

        }else{
            $map['userid']=$this->userID;
            $list = M('Member')->where($map)->find();
            $this->assign('list',$list);
            $this->display();
        }         
    }      
}